<template>
  <div
    class="player-box"
    :style="{
      background: props.background,
    }"
  >
    <div class="player-box-right">
      <div class="right-top">
        <span class="audio-name" :style="{ color: props.nameColor }">{{ getName() }}</span>
        <div class="right-top-menu">
          <div class="right-top-play" @click="play">
            <i :style="{ color: props.iconColor }" class="doorIcon" v-if="!isPlay">&#xe672;</i>
            <i :style="{ color: props.iconColor }" class="doorIcon" v-else>&#xe671;</i>
          </div>
        </div>
      </div>

      <div class="right-bottom" v-if="isPlay">
        <!-- 前 -->
        <div v-if="isPlay" class="time-box" :style="{ color: props.timeColor }">
          {{ realMusicTime }}
        </div>
        <!-- 进度条 -->
        <div class="right-bottom-center">
          <van-slider v-model="percentage" @drag-end="change" @drag-start="dragStart" style="width: 100%" />
        </div>
        <!-- 后 -->
        <div class="right-bottom-right" :style="{ color: props.timeColor }">{{ totalMusicTime }}</div>
      </div>
    </div>
  </div>
  <audio :src="urls + props.url" ref="audioRef" v-show="false" @ended="onAudioEnded" />
</template>

<script setup lang="ts">
// @ts-nocheck
import { Slider } from 'vant';
import { urls } from '@/utils/request/upload';
import { ref, onMounted, nextTick, reactive, computed, watch } from 'vue';
const emit = defineEmits(['delAudioUrl']);
const audioRef = ref(null);
const isPlay = ref(false);
const realMusicTime = ref('00:00');
const totalMusicTime = ref('00:00');

const percentage = ref(0);
const move = ref(false);
// 进度条请使用样式穿透
const props = defineProps({
  url: {
    type: String,
  },
  name: {
    type: String,
  },
  nameColor: {
    //标题颜色
    type: String,
    default: 'rgba(0, 0, 0, 0.8)',
  },
  timeColor: {
    //事件颜色
    type: String,
    default: 'rgba(38, 41, 51, 1)',
  },
  iconColor: {
    //图标颜色
    type: String,
    default: '#262933',
  },
  background: {
    //背景颜色
    type: String,
    default: '#FFFFFF',
  },
  description: {
    type: String,
  },
  id: {
    type: Number,
  },
  //   audio: {
  //     type: Object,
  //   },
});

const audio = reactive({
  type: 3,
  url: '/369462942949445/audio/20230829/455210145833029.mp3',
  description: '',
  name: '桑原由気,長縄まりあ,高田憂希 - イシュカン・コミュニケーション.mp3',
});
const onAudioEnded = () => {
  // console.log('jieshu');

  setTimeout(() => {
    realMusicTime.value = totalMusicTime.value;
    isPlay.value = false;
  }, 800);
};
// 处理进度条
const getName = () => {
  if (!props.name) {
    return '';
  }
  let str = props.name.split('.mp3');
  // console.log(str);
  return str[0];
};

const del = () => {
  emit('delAudioUrl');
};

const handMusicBar = () => {
  let duration = audioRef.value.duration;
  let x = ((audioRef.value.currentTime / duration) * 100).toFixed(1);
  if (move.value) {
    return;
  }
  percentage.value = Number(x);

  // console.log(percentage.value, '当前播放百分比');
};

// 处理时间
const handlMusicTime = () => {
  //用秒数来显示当前播放进度
  let timeDisplay = Math.floor(audioRef.value.currentTime); //获取实时时间
  //分钟
  let minute = parseInt(timeDisplay / 60);
  if (minute < 10) {
    minute = '0' + minute;
  }
  //秒
  let second = Math.round(timeDisplay % 60);
  if (second < 10) {
    second = '0' + second;
  }
  realMusicTime.value = minute + ':' + second;
};

const watchMusicTime = () => {
  let music = audioRef.value;

  music.addEventListener(
    'timeupdate',
    () => {
      handlMusicTime();
      nextTick(() => {
        handMusicBar();
      });
    },
    false,
  );
  // 播放完毕
  music.addEventListener('ended', () => {
    //this.switchMusic(); // 自动播放
  });
  // 捕获音频文件已准备完毕
  // 当媒体文件可以播放的时候会触发oncanplay事件,也可以用oncanplay
  music.oncanplaythrough = () => {
    let time = audioRef.value.duration;
    //分钟
    let minutes = parseInt(time / 60);
    if (minutes < 10) {
      minutes = '0' + minutes;
    }
    //秒
    let seconds = Math.round(time % 60);
    if (seconds < 10) {
      seconds = '0' + seconds;
    }
    totalMusicTime.value = minutes + ':' + seconds;
  };
};
// 处理点击进度条事件
const handClickBar = (e) => {
  audioRef.value.currentTime = (percentage.value / 100) * audioRef.value.duration; // 计算播放时间 位置百分比*总时间
  nextTick(() => {
    audioRef.value.play();
    isPlay.value = true;
  });
};
const change = () => {
  move.value = false;
  handClickBar();
};
const dragStart = () => {
  move.value = true;
};
const play = () => {
  if (audioRef.value.paused) {
    audioRef.value.play();
    isPlay.value = true;
  } else {
    audioRef.value.pause();
    isPlay.value = false;
  }
};
onMounted(() => {
  watchMusicTime();
});
watch(
  () => props.id,
  () => {
    watchMusicTime();
    // play();
    console.log('55');

    nextTick(() => {
      isPlay.value = false;
      audioRef.value.currentTime = 0;
    });
  },
  { deep: true },
);
</script>

<style scoped lang="less">
.player-box {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 56px;
  border-radius: 4px;
  padding: 16px 18px;
  box-shadow: 4px 0px 24px -12px rgba(208, 219, 229, 0.5);
}

.box {
  animation: scroll 5s linear infinite;
}
.player-box-right {
  display: flex;
  flex-direction: column;
  margin-left: 12px;
  // flex: 1;
  width: 100%;
  padding-right: 4px;
}
.right-top {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .audio-name {
    max-width: 80%;

    width: 160px;
    height: 24px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 16px;
    color: #26464e;
    line-height: 24px;
    text-align: left;
    font-style: normal;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.right-top-menu {
  display: flex;
  align-items: center;
  .doorIcon {
    font-size: 24px;
    line-height: 24px;
  }
  .leftIcon {
    cursor: pointer;
    color: rgba(0, 0, 0, 0.38);
  }
}
.right-top-play {
  display: flex;
  margin-left: 16px;
  .doorIcon {
    font-size: 24px;
    line-height: 24px;
    cursor: pointer;
  }
}
.time-box {
  margin-right: 8px;
  width: 38px;
  height: 22px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.38);
  line-height: 22px;
  text-align: center;
  font-style: normal;
}
.right-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin-top: 15px;
}
.right-bottom-left {
  font-size: 20px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(38, 41, 51, 1);
  line-height: 32px;
}
.right-bottom-center {
  flex: 1;
  :deep(.van-slider) {
    width: 100%;
    background: rgba(177, 177, 177, 0.2);
  }
  :deep(.van-slider__bar) {
    background: #826550;
  }
  :deep(.van-slider__button) {
    width: 10px;
    height: 10px;
  }
}
.right-bottom-right {
  margin-left: 8px;
  width: 38px;
  height: 22px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.38);
  line-height: 22px;
  text-align: center;
  font-style: normal;
}
</style>
